package cyh.service.array;

import cyh.domain.dto.BaseDto;
import cyh.domain.dto.DeptDto;
import cyh.domain.dto.RoleDto;
import cyh.domain.entity.Dept;
import cyh.domain.vo.DeptVo;
import cyh.domain.vo.Result;
import cyh.service.IDeptService;
import cyh.service.IUserService;



public class DeptService extends  BaseService<Dept, DeptDto, DeptVo> implements IDeptService {

    private IUserService userService;

    private static  IDeptService singleton;

    private  DeptService() {
        data= new Dept[DATE_SIZE];
        temp= new DeptVo[DATE_SIZE];
    }

    public static IDeptService getInstance() {
        if (singleton == null) {
            singleton = new DeptService();
        }
        return singleton;
    }

    DeptVo convertToVo(Dept dept) {
        return new DeptVo(dept.getId(), dept.name, dept.phone);
    }

    Dept createEntity(DeptDto dto, int id) {

        return new Dept(id,dto.name, dto.phone);
    }

    void updateEntity(DeptDto dto, Dept dept) {
        dept.name = dto.name;
        dept.phone = dto.phone;
    }

    @Override
    public Result delete(int id) {
        if (!existById(id)) return Result.failure("删除失败，部门不存在");
        if (userService.deptInUse(id)) return Result.failure("删除失败，还有用户属于该部门，不能删除");
        data[id] = null;
        return Result.success("删除成功", null);
    }


}